package com.ketai.util;

import com.alibaba.fastjson.JSON;
import com.ketai.common.PageResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author:长沙科泰梦想学院-蓝色小药丸
 * @Mission:打造演说·创客型跨境电商与互联网人才
 * @Description: 发送到前端Json的工具类
 * @Date:Create In 2022/8/4 20:59
 **/
public class SendUtil {
    //发送有返回值的json数据给前端
    public static void Sendjson(HttpServletResponse response, Object data) throws IOException {
        response.setCharacterEncoding("UTF-8");
        //通过流的形式发送json数据
        PrintWriter out = response.getWriter();
        String  ss =JSON.toJSONString(data);
        System.out.println("传到前端："+ss);
        out.write(ss);//通过流的形式响应
    }

    /**
     *  指定日期类型的输出格式
     * @param response
     * @param data
     * @throws IOException
     */
    public static void SendjsonFormart(HttpServletResponse response, Object data) throws IOException {
        response.setCharacterEncoding("UTF-8");
        //通过流的形式发送json数据
        PrintWriter out = response.getWriter();
        String  ss =JSON.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss");
        System.out.println("传到前端："+ss);
        out.write(ss);//通过流的形式响应
    }



    //发送分页数据给前端
    public static void SendPageJson(HttpServletResponse response, Pages data) throws IOException {
        response.setCharacterEncoding("UTF-8");
        PageResult result = new PageResult(true,2000,"加载成功",data.getDataItems(),data.getDataTotal());
        //通过流的形式发送json数据
        PrintWriter out = response.getWriter();
        String  ss =JSON.toJSONString(result);
        System.out.println("传到前端："+ss);
        out.write(ss);//通过流的形式响应
    }
}
